package net.itgoo.parkingpay.ui.signin;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.bigkoo.svprogresshud.SVProgressHUD;

import net.itgoo.parkingpay.R;
import net.itgoo.parkingpay.ui.main.ParkingMainActivity;
import net.itgoo.parkingpay.vendor.widget.fragment.ParkingBaseFragment;
import net.itgoo.titlebar.TitleBar;

import hk.ids.gws.android.sclick.SClick;

public class ParkingSigninFragment extends ParkingBaseFragment implements ParkingSigninContract.View {
    private ParkingSigninContract.Presenter mPresenter;
    private TitleBar mTitleBar;
    private EditText mUsernameEditText;
    private EditText mPasswordEditText;
    private SVProgressHUD mHUD;

    public ParkingSigninFragment() {
        // Required empty public constructor
    }

    public static ParkingSigninFragment newInstance(Bundle bundle) {
        ParkingSigninFragment fragment = new ParkingSigninFragment();
        if (bundle != null) {
            fragment.setArguments(bundle);
        }
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mPresenter != null) {
            mPresenter.start();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.stop();
        }
    }

    @Override
    public void setPresenter(ParkingSigninContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    protected int getLayoutResourceID() {
        return R.layout.parking_fragment_signin;
    }

    @Override
    protected void initUI(Bundle savedInstanceState) {
        initViews();
        initTitleBar();
        initHUD();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    private void initViews() {
        View contentView = getContentView();
        mTitleBar = contentView.findViewById(R.id.parking_view_title_bar_ct);
        mUsernameEditText = contentView.findViewById(R.id.parking_fragment_signin_username_et);
        mPasswordEditText = contentView.findViewById(R.id.parking_fragment_signin_pwd_et);
        contentView.findViewById(R.id.parking_fragment_signin_ok_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!SClick.check(SClick.BUTTON_CLICK)) return;

                mPresenter.signIn(mUsernameEditText.getText().toString(), mPasswordEditText.getText().toString());
            }
        });
    }

    private void initTitleBar() {
        mTitleBar.setTitle(getString(R.string.parking_pay));
        mTitleBar.setLeftVisible(false);
    }

    private void initHUD() {
        mHUD = new SVProgressHUD(getActivity());
    }

    @Override
    public void showLoading() {
        mHUD.showWithStatus("");
    }

    @Override
    public void showSignInSuccess() {
        mHUD.dismissImmediately();

        Intent intent = new Intent(getActivity(), ParkingMainActivity.class);
        startActivity(intent);
        mPasswordEditText.setText("");
    }

    @Override
    public void showSignInFailure(String error) {
        mHUD.dismissImmediately();
        mHUD.showErrorWithStatus(error);
    }
}
